/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.JeuListener;
import Modele.Dictionnaire;
import Modele.Grille;
import Modele.SolverV2;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Polygon;
import java.util.TreeSet;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Gloire
 */
public class Jeu extends JPanel{
    static Polygon[][] polygons;
	private Grille grille;
	private LabelCase[][] labels;
	private int[] mot;
	private int motPos;
	private int motLength;
	private JLabel score;
	private JLabel chrono;
	private int chronoInt;
	private int scoreInt;

	private TreeSet<String> dico;
	private TreeSet<String> solution;
	private boolean finish;

	public Jeu(Grille grille, Font font, Dictionnaire dico, JLabel jscore,
			JLabel jchrono, int chrono) {
		this(grille, font, dico, jscore);
		this.chrono = jchrono;
		this.chronoInt = chrono;
		chrono();
	}

	public Jeu(Grille grille, Font font, Dictionnaire dico, JLabel jscore) {
		this.grille= grille;
		this.mot = new int[16];
		this.dico = dico.getDico();
		this.solution = new SolverV2(grille.getGrille()).getResult();
		setFont(font);
		setLayout(new GridBagLayout());
		setPreferredSize(new Dimension(700, 500));
		generePolygon();
		JeuListener listener = new JeuListener(this);
		addMouseListener(listener);
		addMouseMotionListener(listener);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = 0;
		gbc.weighty = 1;

		printLetters(this, gbc);
		score = jscore;
	}
        
        public Jeu(Grille grille, Dictionnaire dico, JLabel jscore) {
		this.grille= grille;
		this.mot = new int[16];
		this.dico = dico.getDico();
		this.solution = new SolverV2(grille.getGrille()).getResult();
		setLayout(new GridBagLayout());
		setPreferredSize(new Dimension(700, 500));
		generePolygon();
		JeuListener listener = new JeuListener(this);
		addMouseListener(listener);
		addMouseMotionListener(listener);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = 0;
		gbc.weighty = 1;

		//printLetters(this, gbc);
		score = jscore;
	}

	private void printLetters(JPanel panel, GridBagConstraints gbc) {
		int y1, y2;
		int pos_i = 0;
                int pos_j = 0;
		int margeX = 23, margeY = 25;
		labels = new LabelCase[4][4];
		/*for (int i = 0; i < 4; i++) {
			if (i == 0 || i == 2) {
				y1 = 2 * i;
				y2 = 2 * i + 1;
			} else {
				y2 = 2 * i;
				y1 = 2 * i + 1;
			}
			gbc.gridwidth = 1;
			for (int j = 0; j < 7; j++) {
				if (!((i == 0 || i == 3) && (j == 0 || j == 6))) {
					gbc.gridx = j;
					gbc.gridy = (j % 2 == 0 ? y1 : y2);
					if ((i + j) % 2 == 0)
						gbc.insets = new Insets(margeY, margeX, 0, margeX);
					else
						gbc.insets = new Insets(0, margeX, margeY, margeX);

					LabelCase label = new LabelCase(
							(char) (grille.getCase(pos_i,pos_j).getValeur()),grille.getCase(pos_i,pos_j).getPoints(),getFont());
					labels[pos_i] = label;

					panel.add(labels[pos], gbc);
					labels[pos].setFont(getFont());
					pos_i++;
				}
                                pos_i = 0;
                                pos_j++;
			}
		}*/
	}

	private void generePolygon() {
		polygons = new Polygon[4][4];

		
	}

	/*public void paintComponent(Graphics g) {
		g.drawImage(Fenetre.createIcon("grid.png", "grille").getImage(), 0, 0,
				null);
	}*/

	private void chrono() {
		new java.util.Timer().schedule(new java.util.TimerTask() {
			@Override
			public void run() {
				if (--chronoInt > 0 && !finish) {
					chrono.setText("chrono : " + chronoInt);
					chrono();
				}
			}
		}, 1000);
	}

	public void pointInPolygons(Point point) {
		for (int i = 0; i < polygons.length; i++) {
			if (polygons[i][i].contains(point)
					&& !grille.getCase(i,i).estVisitee()) {
				grille.getCase(i,i).setVisitee(true);
				labels[i][i].setColor(1);
				mot[motPos++] = i;
				repaint();
				break;
			}
		}
	}

	/*private boolean find(String s, int i) {
		if (motPos == i) {
			if (!modele.Noeud.isInDico(s, arbreMots)
					&& Noeud.isInDico(s.toString(), arbreLexico)) {
				arbreMots = (Noeud.stringToAbl(s, arbreMots));
				scoreInt += s.length();
				return true;
			} else
				return false;
		} else if (graph.getSommet(mot[i]).getVal() == '?') {
			boolean b = false;
			for (int j = 0; j < 26; j++) {
				b |= find(s + (char) (j + 97), i + 1);
			}
			return b;
		} else {
			return find(s + graph.getSommet(mot[i]), i + 1);
		}
	}*/

	/*public void mouseReleased() throws InterruptedException {
		for (int i = 0; i < polygons.length; i++) {
			graph.getSommet(i).setVisited(false);
			labels[i].setColor(0);
		}
		motLength = motPos;

		if (find("", 0))
			for (int i = 0; i < motPos; i++)
				labels[mot[i]].setColor(2);
		else
			for (int i = 0; i < motPos; i++)
				labels[mot[i]].setColor(3);
		score.setText("Score : " + scoreInt);
		motPos = 0;
		repaint();
	}*/

	public TreeSet<String> getDico() {
		return dico;
	}

	/*public Noeud getArbreMots() {
		return arbreMots;
	}

	public void resetLabels() {
		for (int i = 0; i < motLength; i++)
			labels[mot[i]].setColor(0);
	}

	public void finish() {
		finish = true;
	}*/
}
